<?php
namespace Admin\Model;
use Think\Model;
class CommentModel extends Model 
{
	protected $insertFields = array('member_id','goods_id','content','star','good_number');
	protected $updateFields = array('id','member_id','goods_id','content','star','good_number');
	protected $_validate = array(
		array('member_id', 'require', '会员id不能为空！', 1, 'regex', 3),
		array('member_id', 'number', '会员id必须是一个整数！', 1, 'regex', 3),
		array('goods_id', 'require', '商品id不能为空！', 1, 'regex', 3),
		array('goods_id', 'number', '商品id必须是一个整数！', 1, 'regex', 3),
		array('content', 'require', '评论的内容不能为空！', 1, 'regex', 3),
		array('content', '1,300', '评论的内容的值最长不能超过 300 个字符！', 1, 'length', 3),
		array('star', 'require', '评论的分值不能为空！', 1, 'regex', 3),
		array('star', 'number', '评论的分值必须是一个整数！', 1, 'regex', 3),
		array('good_number', 'number', '有用的数字必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($member_id = I('get.member_id'))
			$where['member_id'] = array('eq', $member_id);
		if($goods_id = I('get.goods_id'))
			$where['goods_id'] = array('eq', $goods_id);
		if($addtime = I('get.addtime'))
			$where['addtime'] = array('eq', $addtime);
		if($content = I('get.content'))
			$where['content'] = array('like', "%$content%");
		if($star = I('get.star'))
			$where['star'] = array('eq', $star);
		if($good_number = I('get.good_number'))
			$where['good_number'] = array('eq', $good_number);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}